package com.theatre.userservice.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.theatre.userservice.annotation.RequirePermission;
import com.theatre.userservice.mapper.ScriptCategoryMapper;
import com.theatre.userservice.model.entity.Script;
import com.theatre.userservice.model.entity.ScriptCategory;
import com.theatre.userservice.model.vo.Result;
import com.theatre.userservice.service.ScriptCategoryService;
import com.theatre.userservice.service.ScriptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/script-category")
@Tag(name = "分类接口", description = "提供剧本分类相关接口")
@Slf4j
public class ScriptCategotyController {
    @Autowired
    private ScriptCategoryMapper scriptCategoryMapper;
    @Autowired
    private ScriptService scriptService;

    @PostMapping("/add")
    @RequirePermission("content:manage")
    @Operation(summary = "新增剧本分类", description = "新增一个剧本分类")
    public Result<String> add(@RequestBody ScriptCategory scriptCategory) {
        log.info("新增剧本分类");
        scriptCategoryMapper.insert(scriptCategory);
        return Result.success();
    }

    @GetMapping("/get")
    @Operation(summary = "查询所有剧本（分页）", description = "分页查询所有剧本")
    public Result<Page<Script>> getAll(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        Page<Script> scriptPage = new Page<>(page, size);
        Page<Script> result = scriptService.page(scriptPage);
        return Result.success(result);
    }

    @GetMapping("/getByCategory")
    @Operation(summary = "根据分类ID查询剧本（分页）", description = "分页查询分类下的剧本")
    public Result<Page<Script>> getByCategory(
            @RequestParam Long categoryId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        Page<Script> scriptPage = new Page<>(page, size);
        Page<Script> result = scriptService.page(scriptPage,
                new LambdaQueryWrapper<Script>().eq(Script::getCategoryId, categoryId));

        if (result.getRecords().isEmpty()) {
            return Result.error("该分类下暂无剧本");
        }
        return Result.success(result);
    }
}
